using System;
using System.ComponentModel;
using System.Web;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

namespace FCS_Workflows
{
    public sealed partial class PrimaryStateMachine : StateMachineWorkflowActivity
    {
        #region Constants
        private const string FCS_TRACKING_NUMBER_KEY = "fcs_tracking_number";
        #endregion Constants

        #region Public properties
        #region Workflow
        public String LogHistoryDescription = default(System.String);
        public String LogHistoryOutcome = default(System.String);
        public FCSWorkflowConfig workflowConfig;
        public SPWorkflowActivationProperties workflowProperties = new Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties();
        #endregion

        #region Tasks
        // FCS intake task content type formID
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [CategoryAttribute("FCS")]
        public String FCS_ContentTypeId
        {
            get
            {
                return "0x010801000F14551F35854de7B72671CB134FBCF9";
            }
            set
            {
                // Do nothing, we have a fixed content formID
            }
        }

        public SPWorkflowTaskProperties latest_AfterProperties;
        #endregion Tasks

        #region Email
        public String EmailFromAddress = default(System.String);
        public String EmailBody = default(System.String);
        public String EmailSubject = default(System.String);
        public String EmailToAddress = default(System.String);
        #endregion
        #endregion Public...

        #region Private properites
        private string FormID
        {
            get
            {
                return WorkflowSupport.GetFormIntakeID(workflowProperties.Item.File);
            }
        }

        private string FormLink
        {
            get
            {
                return "<a href=\"" + FormUrl + "\">" + HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Intake_Form_Link").ToString() + "</a>";
            }
        }

        private string FormRequestName
        {
            get
            {
                return WorkflowSupport.GetFormRequestName(workflowProperties.Item.File);
            }
        }

        private string FormRequestType
        {
            get
            {
                return WorkflowSupport.GetFormRequestType(workflowProperties.Item.File);
            }
        }

        private string FormTrackingNumber
        {
            get
            {
                int formCount = 0;
                string key = FCS_TRACKING_NUMBER_KEY;
                try
                {
                    // Add 1 to the value of the key found in the web properties bag
                    formCount = int.Parse(workflowProperties.List.ParentWeb.Properties[key]) + 1;
                    workflowProperties.List.ParentWeb.Properties[key] = formCount.ToString();
                    workflowProperties.List.ParentWeb.Properties.Update();
                }
                catch
                {
                    // Create the key if it doesn't exist and set the value of 1
                    workflowProperties.List.ParentWeb.Properties.Add(key, "1");
                    workflowProperties.List.ParentWeb.Properties.Update();
                    formCount = 1;
                }

                string formTrackingNumber = FormRequestType + " - " + DateTime.Today.Year.ToString() + " - " + formCount.ToString();

                return formTrackingNumber;
            }
        }

        private string FormUrl
        {
            get
            {
                return (workflowProperties.Web.Url + "/" + workflowProperties.Item.Url).Replace(" ", "%20");
            }
        }
        #endregion Private...

        #region Constructor

        public PrimaryStateMachine()
        {
            InitializeComponent();
        }

        #endregion Constructor

        #region Exception handler

        protected override ActivityExecutionStatus HandleFault(ActivityExecutionContext executionContext, Exception exception)
        {
            try
            {
                WorkflowSupport.LogException("PrimaryStateMachine Workflow", exception);

                // Write the error to the workflow's associated history list
                workflowProperties.Workflow.CreateHistoryEvent(
                    (int)SPWorkflowHistoryEventType.WorkflowError,   // int eventID
                    0,  // object groupID
                    workflowProperties.OriginatorUser,  // SPMember user
                    "Error Occurred",   // string outcomde 
                    string.Format("An exception occured during activity {0}. Check the item data for more information.", executionContext.Activity.Name),   // string description
                    WorkflowSupport.FormatException(exception)  // string otherData
                    );
            }
            catch
            {
            }

            return base.HandleFault(executionContext, exception);
        }

        #endregion

        #region IfElseActivity code conditions

        private void AnalystIsAssigned(object sender, ConditionalEventArgs e)
        {
            e.Result = false;
            string analyst = WorkflowSupport.GetTaskAnalyst(latest_AfterProperties);

            if (!string.IsNullOrEmpty(analyst))
            {
                e.Result = true;
            }
        }

        private void EmailIsNeeded(object sender, ConditionalEventArgs e)
        {
            e.Result = false;
            if (!string.IsNullOrEmpty(workflowConfig.EmailToAddress) && !string.IsNullOrEmpty(workflowConfig.EmailToAddress))
            {
                e.Result = true;
            }
        }

        private void TaskIsCancelled(object sender, ConditionalEventArgs e)
        {
            e.Result = false;
            FCSWorkflowTaskStatus curStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);

            if (curStatus == FCSWorkflowTaskStatus.Cancelled)
            {
                e.Result = true;
            }
        }

        private void TaskIsCompleted(object sender, ConditionalEventArgs e)
        {
            e.Result = false;
            FCSWorkflowTaskStatus curStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);

            if (curStatus == FCSWorkflowTaskStatus.Completed)
            {
                e.Result = true;
            }
        }

        private void TaskIsProcessing(object sender, ConditionalEventArgs e)
        {
            e.Result = false;
            FCSWorkflowTaskStatus curStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);

            if (curStatus == FCSWorkflowTaskStatus.Processing)
            {
                e.Result = true;
            }
        }

        private void TaskIsReassigned(object sender, ConditionalEventArgs e)
        {
            e.Result = false;
            FCSWorkflowTaskStatus curStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
            if (curStatus == FCSWorkflowTaskStatus.Reassigned)
            {
                e.Result = true;
            }
        }

        private void TaskIsRejected(object sender, ConditionalEventArgs e)
        {
            e.Result = false;
            FCSWorkflowTaskStatus curStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
            if (curStatus == FCSWorkflowTaskStatus.Rejected)
            {
                e.Result = true;
            }
        }

        private void WorkflowIsNone(object sender, ConditionalEventArgs e)
        {
            e.Result = false;
            if (workflowConfig.WorkflowType == FCSWorkflowType.None)
            {
                e.Result = true;
            }
        }

        private void WorkflowIsNoReviewNoApprove(object sender, ConditionalEventArgs e)
        {
            e.Result = false;
            if (workflowConfig.WorkflowType == FCSWorkflowType.NoReviewNoApprove)
            {
                e.Result = true;
            }
        }

        private void WorkflowIsReviewNoApprove(object sender, ConditionalEventArgs e)
        {
            e.Result = false;
            if (workflowConfig.WorkflowType == FCSWorkflowType.ReviewNoApprove)
            {
                e.Result = true;
            }
        }

        private void WorkflowIsReviewAndApprove(object sender, ConditionalEventArgs e)
        {
            e.Result = false;
            if (workflowConfig.WorkflowType == FCSWorkflowType.ReviewAndApprove)
            {
                e.Result = true;
            }
        }

        #endregion IfElseActivity code conditions

        #region State code

        #region BeginState

        #endregion BeginState

        #region RequestedState

        #region Task properties
        // Create Task
        public Guid ctRequest_TaskId = default(System.Guid);
        public SPWorkflowTaskProperties ctRequest_TaskProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        // On Task Changed
        public SPWorkflowTaskProperties otcRequest_AfterProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties otcRequest_BeforeProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        #endregion Task properties

        #region State initialization activity
        private void caRequested_Init_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            string activityLogNewIntake = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Activity_Log_New_Intake").ToString() + ": '" + FormID + "'";
            string activityLogStatusChange = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Activity_Log_Request_Status_Requested").ToString();
            string formID = FormID;
            string formTrackingNumber = FormTrackingNumber;  // Generate the form tracking number
            string formUrl = FormUrl;
            string user = workflowProperties.OriginatorUser.Name;
            string requestStatusRequested = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Requested_State_Name").ToString();
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion

            #region Copy attachments
            WorkflowSupport.CopyAttachmentsToArtifactsLibrary(workflowProperties, formID);
            #endregion

            #region Update form library
            // Write to the form library
            WorkflowSupport.WriteFormValuesToFormLibrary(workflowProperties, formTrackingNumber, formUrl);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLog(workflowProperties, user, activityLogNewIntake, formID, formTrackingNumber);
            WorkflowSupport.WritieToAuditLog(workflowProperties, user, activityLogStatusChange, formID, formTrackingNumber);

            // Request Summaries
            WorkflowSupport.WriteFormValuesToRequestSummariesList(workflowProperties, formTrackingNumber, formUrl);
            WorkflowSupport.WriteToRequestSummariesListStatus(workflowProperties, formID, requestStatusRequested);
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, float.Parse("0"));
            WorkflowSupport.WriteToRequestSummariesListWorkflowComplete(workflowProperties, formID, false);

            // Location list
            WorkflowSupport.WriteFormValuesToLocationList(workflowProperties, formTrackingNumber, formUrl);

            // What's New

            #endregion

            #region Update form
            // (This method call needs to be last!!! - see http://hristopavlov.wordpress.com/2008/05/14/uploading-a-file-event-receivers-the-file-has-been-modified-by/)

            // Update the document tracking number
            string requestType = FormRequestType;
            WorkflowSupport.SetFormTrackingNumber(workflowProperties, requestType, formTrackingNumber);

            // Update the document status
            WorkflowSupport.SetFormStatus(workflowProperties, requestStatusRequested);

            // Update the log

            #endregion
        }

        private void seRequested_SendEmail_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            string requestPriority = WorkflowSupport.GetFormRequestPriority(workflowProperties.Item.File);
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion

            if (workflowConfig.EmailHighPriorityOnly)
            {
                if (!(requestPriority.ToUpper().Equals("HIGH")))
                {
                    return;
                }
            }

            if (!string.IsNullOrEmpty(workflowConfig.EmailToAddress) && !string.IsNullOrEmpty(workflowConfig.EmailToAddress))
            {
                string emailSubject = workflowConfig.EmailSubject;
                if (string.IsNullOrEmpty(emailSubject))
                {
                    // Grab the default email subject
                    emailSubject = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Default_Email_Subject").ToString();
                }

                string emailBody = workflowConfig.EmailBody;
                if (string.IsNullOrEmpty(emailBody))
                {
                    // Grab the default email body
                    emailBody = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Default_Email_Body").ToString();
                }

                EmailToAddress = workflowConfig.EmailToAddress;
                EmailFromAddress = workflowConfig.EmailToAddress;
                EmailSubject = workflowConfig.EmailSubject;
                EmailBody = workflowConfig.EmailBody;
            }
        }

        private void ctRequested_CreateTask_NoReviewNoApproveWorkflow_MethodInvoking(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File); ;
            string taskDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "New_Service_Request_Task_Description").ToString() + "<br /><br />" + FormLink;
            string taskStatusDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Task_Status_Open").ToString();
            string taskTitle = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "New_Service_Request_Task_Title").ToString() + ": " + FormRequestName;
            #endregion

            #region Create a new task
            ctRequest_TaskId = Guid.NewGuid();
            ctRequest_TaskProperties = new SPWorkflowTaskProperties();

            // Base properties
            ctRequest_TaskProperties.Title = taskTitle;
            ctRequest_TaskProperties.PercentComplete = 0;
            ctRequest_TaskProperties.StartDate = DateTime.Now;
            ctRequest_TaskProperties.DueDate = DateTime.Now.AddMinutes(30);
            ctRequest_TaskProperties.Description = taskDescription;

            // Extended properties
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("df9a625c-1c2f-425f-9b7c-8dc278433e13"), formID);  // Intake ID
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("a2deeb92-804a-4d37-b0e0-0b4352111574"), formTrackingNumber);  // Intake Tracking Number
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("3f0daace-0560-4bdb-8b03-c3bb82c56252"), ";#" + workflowConfig.AssignStaffFunction + ";#"); // Staff Function
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("9bad9167-6317-4c2f-b165-be7af7d76f1d"), taskStatusDescription);  // Intake Task Status
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("9d924f10-6e2e-49da-ac89-25637432c8b2"), null);    // Intake Analyst
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("752ce062-37c9-417e-bb03-32e4139b896f"), "");  // Intake Task Notes
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, FCSWorkflowTaskStatus.Open, formID, formTrackingNumber);
            #endregion
        }

        private void ctRequested_CreateTask_ElseWorkflow_MethodInvoking(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string activityLogTaskOpened = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Activity_Log_Task_Opened").ToString() + ": '" + FormID + "'";
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string user = workflowProperties.OriginatorUser.Name;
            string taskDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Assign_Service_Request_Task_Description").ToString() + "<br /><br />" + FormLink;
            string taskStatusDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Task_Status_Open").ToString();
            string taskTitle = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Assign_Service_Request_Task_Title").ToString() + ": " + FormRequestName;
            #endregion

            #region Create a new task
            ctRequest_TaskId = Guid.NewGuid();
            ctRequest_TaskProperties = new SPWorkflowTaskProperties();

            // Base properties
            ctRequest_TaskProperties.Title = taskTitle;
            ctRequest_TaskProperties.PercentComplete = 0;
            ctRequest_TaskProperties.StartDate = DateTime.Now;
            ctRequest_TaskProperties.DueDate = DateTime.Now.AddMinutes(30);
            ctRequest_TaskProperties.Description = taskDescription;

            // Extended properties
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("df9a625c-1c2f-425f-9b7c-8dc278433e13"), formID);  // Intake ID
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("a2deeb92-804a-4d37-b0e0-0b4352111574"), formTrackingNumber);  // Intake Tracking Number
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("3f0daace-0560-4bdb-8b03-c3bb82c56252"), ";#" + workflowConfig.AssignStaffFunction + ";#"); // Staff Function
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("9bad9167-6317-4c2f-b165-be7af7d76f1d"), taskStatusDescription);  // Intake Task Status
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("9d924f10-6e2e-49da-ac89-25637432c8b2"), null);    // Intake Analyst
            ctRequest_TaskProperties.ExtendedProperties.Add(new Guid("752ce062-37c9-417e-bb03-32e4139b896f"), "");  // Intake Task Notes
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLog(workflowProperties, user, activityLogTaskOpened, formID, formTrackingNumber);
            #endregion
        }
        #endregion

        #region Event driven activity
        private void otcRequested_TaskCreated_Invoked(object sender, ExternalDataEventArgs e)
        {
            try
            {
                #region Variables
                workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

                latest_AfterProperties = otcRequest_AfterProperties;

                string formID = FormID;
                string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);

                FCSWorkflowTaskStatus taskStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
                #endregion

                #region Update lists
                // Activity Log
                WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, taskStatus, formID, formTrackingNumber);
                #endregion
            }
            catch (Exception ex)
            {
                WorkflowSupport.LogException("otcRequested_Invoked", ex);
            }
        }

        private void caRequested_TaskCompleted_NoReviewNoApproveWorkflow_ExecuteCode(object sender, EventArgs e)
        {
            // This needs to be done here because for this workflow type the next state is "Closed"

            #region Variables
            string activityLogStatusChange = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Activity_Log_Request_Status_Assigned").ToString();
            string analyst = WorkflowSupport.GetTaskAnalyst(latest_AfterProperties);
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string user = workflowProperties.OriginatorUser.Name;
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion

            #region Update form library
            // Write to the form library
            WorkflowSupport.WriteToFormLibraryAnalyst(workflowProperties, analyst);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLog(workflowProperties, user, activityLogStatusChange, formID, formTrackingNumber);
            #endregion
        }

        private void caRequested_TaskCompleted_AnalystAssigned_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            string analyst = WorkflowSupport.GetTaskAnalyst(latest_AfterProperties);
            string formID = FormID;
            #endregion

            #region Update lists
            // Request Summaries
            string requestStatusAssigned = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Assigned_State_Name").ToString();
            WorkflowSupport.WriteToRequestSummariesListStatus(workflowProperties, formID, requestStatusAssigned);
            WorkflowSupport.WriteToRequestSummariesListAnalyst(workflowProperties, formID, analyst);
            #endregion
        }

        private void caRequested_TaskCompleted_AnalystNotAssigned_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            string formID = FormID;
            #endregion

            #region Update lists
            // Request Summaries
            string requestStatusRequested = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Requested_State_Name").ToString();
            WorkflowSupport.WriteToRequestSummariesListStatus(workflowProperties, formID, requestStatusRequested);
            #endregion
        }

        private void caRequested_TaskProcessing_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string user = workflowProperties.OriginatorUser.Name;
            float percentageCompleted = latest_AfterProperties.PercentComplete;

            FCSWorkflowTaskStatus taskStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, taskStatus, formID, formTrackingNumber);

            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, percentageCompleted);
            #endregion
        }

        #endregion Event driven activity

        #region State finalization activity
        private void caRequested_Final_ExecuteCode(object sender, EventArgs e)
        {
        }
        #endregion
        #endregion

        #region AssignedState
        #region Task properties
        // Create Task
        public SPWorkflowTaskProperties ctAssigned_TaskProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties utAssigned_TaskProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public Guid ctAssigned_TaskId = default(System.Guid);
        // On Task Changed
        public SPWorkflowTaskProperties otcAssigned_AfterProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties otcAssigned_BeforeProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        #endregion Task properties

        #region State initialization activity
        private void caAssignedInit_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string activityLogStatusChange = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Activity_Log_Request_Status_Assigned").ToString();
            string analyst = WorkflowSupport.GetTaskAnalyst(latest_AfterProperties);
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string user = workflowProperties.OriginatorUser.Name;
            string requestStatus = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Assigned_State_Name").ToString();
            #endregion

            #region Update form library
            // Write to the form library
            WorkflowSupport.WriteFormValuesToFormLibrary(workflowProperties, formTrackingNumber, FormUrl);
            WorkflowSupport.WriteToFormLibraryStatus(workflowProperties, requestStatus);

            // Write the analyst to the form library column
            WorkflowSupport.WriteToFormLibraryAnalyst(workflowProperties, analyst);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLog(workflowProperties, user, activityLogStatusChange, formID, formTrackingNumber);

            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListStatus(workflowProperties, formID, requestStatus);
            WorkflowSupport.WriteToRequestSummariesListAnalyst(workflowProperties, formID, analyst);
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, float.Parse("0"));

            #endregion Update lists

            #region Update form
            // (These method calls needs to be last!!! - see http://hristopavlov.wordpress.com/2008/05/14/uploading-a-file-event-receivers-the-file-has-been-modified-by/)

            // Update the analyst
            WorkflowSupport.SetFormAnalyst(workflowProperties, analyst);

            // Update the status
            WorkflowSupport.SetFormStatus(workflowProperties, requestStatus);

            // Update the log

            #endregion
        }

        private void ctAssigned_CreateTask_MethodInvoking(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string analyst = WorkflowSupport.GetTaskAnalyst(latest_AfterProperties);
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string taskDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Analyze_Service_Request_Task_Description").ToString() + "<br /><br />" + FormLink;
            string taskNotes = WorkflowSupport.GetTaskNotes(latest_AfterProperties);
            string taskTitle = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Analyze_Service_Request_Task_Title").ToString() + ": " + FormRequestName;
            string taskStatusDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Task_Status_Open").ToString();
            #endregion

            #region Create a new task
            ctAssigned_TaskId = Guid.NewGuid();
            ctAssigned_TaskProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();

            // Base properties
            ctAssigned_TaskProperties.Title = taskTitle;
            ctAssigned_TaskProperties.StartDate = DateTime.Now;
            ctAssigned_TaskProperties.DueDate = DateTime.Now.AddMinutes(60);
            ctAssigned_TaskProperties.AssignedTo = analyst;
            ctAssigned_TaskProperties.Description = taskDescription;
            ctAssigned_TaskProperties.PercentComplete = 0;

            // Extended properties
            ctAssigned_TaskProperties.ExtendedProperties.Add(new Guid("df9a625c-1c2f-425f-9b7c-8dc278433e13"), formID);  // Intake ID
            ctAssigned_TaskProperties.ExtendedProperties.Add(new Guid("a2deeb92-804a-4d37-b0e0-0b4352111574"), formTrackingNumber);  // Intake Tracking Number
            ctAssigned_TaskProperties.ExtendedProperties.Add(new Guid("3f0daace-0560-4bdb-8b03-c3bb82c56252"), ";#" + workflowConfig.AnalyzeStaffFunction + ";#"); // Staff Function
            ctAssigned_TaskProperties.ExtendedProperties.Add(new Guid("9bad9167-6317-4c2f-b165-be7af7d76f1d"), taskStatusDescription);  // Intake Task Status
            ctAssigned_TaskProperties.ExtendedProperties.Add(new Guid("9d924f10-6e2e-49da-ac89-25637432c8b2"), analyst);    // Intake Analyst
            ctAssigned_TaskProperties.ExtendedProperties.Add(new Guid("752ce062-37c9-417e-bb03-32e4139b896f"), taskNotes);  // Intake Task Notes
            #endregion

            #region Update lists
            WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, FCSWorkflowTaskStatus.Open, formID, formTrackingNumber);
            #endregion
        }
        #endregion

        #region Event driven activity
        private void otcAssigned_TaskChanged_Invoked(object sender, ExternalDataEventArgs e)
        {
            try
            {
                #region Variables
                workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
                latest_AfterProperties = otcAssigned_AfterProperties;

                string formID = FormID;
                string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
                string taskStatusDescriptionProcessing = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Task_Status_Processing").ToString();
                string taskStatusDescriptionReassigned = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Task_Status_Reassigned").ToString();

                FCSWorkflowTaskStatus taskStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
                #endregion

                #region Update lists
                // Activity Log
                WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, taskStatus, formID, formTrackingNumber);

                #endregion
            }
            catch (Exception ex)
            {
                WorkflowSupport.LogException("otcAssigned_Invoked", ex);
            }
        }

        private void caAssigned_TaskCompleted_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File); 
            float percentageCompleted = 1;
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion Variables

            #region Update lists
            // Request Summaries
            // Set the "% Analysis Complete" value to 100
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, percentageCompleted);
            #endregion
        }

        private void caAssigned_TaskProcessing_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File); 
            string user = workflowProperties.OriginatorUser.Name;
            float percentageCompleted = latest_AfterProperties.PercentComplete;

            FCSWorkflowTaskStatus taskStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, taskStatus, formID, formTrackingNumber);

            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, percentageCompleted);
            #endregion
        }

        private void caAssigned_TaskReassigned_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string analyst = WorkflowSupport.GetTaskAnalyst(latest_AfterProperties);
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string user = workflowProperties.OriginatorUser.Name;
            string taskStatusDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Task_Status_Open").ToString();

            FCSWorkflowTaskStatus taskStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
            #endregion

            #region Update form library
            // Write the analyst to the form library column
            WorkflowSupport.WriteToFormLibraryAnalyst(workflowProperties, analyst);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, taskStatus, formID, formTrackingNumber);

            // Request Summaries
            // Updadte the "Assigned Analyst" field
            WorkflowSupport.WriteToRequestSummariesListAnalyst(workflowProperties, formID, analyst);
            #endregion

            #region Update form
            // Update the analyst
            WorkflowSupport.SetFormAnalyst(workflowProperties, analyst);
            #endregion

            #region Update task
            // Change the "Intake Task Status" field to "Open
            utAssigned_TaskProperties.ExtendedProperties[new Guid("9bad9167-6317-4c2f-b165-be7af7d76f1d")] = taskStatusDescription;

            // Change the "Assigned To" field to the analyst
            utAssigned_TaskProperties.AssignedTo = analyst;
            #endregion
        }
        #endregion

        #region State finalization activity

        private void caAssigned_Final_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion
        }
        #endregion
        #endregion

        #region ReviewedState
        #region Task properties
        // Create Task
        public Guid ctReviewed_TaskId = default(System.Guid);
        public SPWorkflowTaskProperties ctReviewed_TaskProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        // On Task Changed
        public SPWorkflowTaskProperties otcReviewed_AfterProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties otcReviewed_BeforeProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        #endregion

        #region State initialization activity
        private void caReviewed_Init_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string activityLogStatusChange = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Activity_Log_Request_Status_Reviewed").ToString();
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File); 
            string formUrl = FormUrl;
            string user = workflowProperties.OriginatorUser.Name;
            string requestStatus = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Reviewed_State_Name").ToString();
            #endregion

            #region Update form library
            // Write to the form libary
            WorkflowSupport.WriteFormValuesToFormLibrary(workflowProperties, formTrackingNumber, formUrl);
            WorkflowSupport.WriteToFormLibraryStatus(workflowProperties, requestStatus);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLog(workflowProperties, user, activityLogStatusChange, formID, formTrackingNumber);

            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListStatus(workflowProperties, formID, requestStatus);
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, float.Parse("0"));
            #endregion

            #region Update form
            // (This method call needs to be last!!! - see http://hristopavlov.wordpress.com/2008/05/14/uploading-a-file-event-receivers-the-file-has-been-modified-by/)

            // Update status
            WorkflowSupport.SetFormStatus(workflowProperties, requestStatus);

            // Update log

            #endregion
        }

        private void ctReviewed_CreateTask_MethodInvoking(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string analyst = WorkflowSupport.GetTaskAnalyst(latest_AfterProperties);
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File); 
            string taskDescription;
            if (workflowConfig.WorkflowType == FCSWorkflowType.ReviewAndApprove)
            {
                taskDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Review_Service_Request_Task_Description_Review_And_Approve").ToString() + "<br /><br />" + FormLink;

            }
            else
            {
                taskDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Review_Service_Request_Task_Description_Review_No_Approve").ToString() + "<br /><br />" + FormLink;

            }
            string taskNotes = WorkflowSupport.GetTaskNotes(latest_AfterProperties);
            string taskStatusDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Task_Status_Open").ToString();
            string taskTitle = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Review_Service_Request_Task_Title").ToString() + ": " + FormRequestName;
            #endregion

            #region Create a new task
            ctReviewed_TaskId = Guid.NewGuid();
            ctReviewed_TaskProperties = new SPWorkflowTaskProperties();

            // Base properties
            ctReviewed_TaskProperties.Title = taskTitle;
            ctReviewed_TaskProperties.StartDate = DateTime.Now;
            ctReviewed_TaskProperties.DueDate = DateTime.Now.AddMinutes(60);
            ctReviewed_TaskProperties.Description = taskDescription;
            ctReviewed_TaskProperties.PercentComplete = 0;

            // Extended properties
            ctReviewed_TaskProperties.ExtendedProperties.Add(new Guid("df9a625c-1c2f-425f-9b7c-8dc278433e13"), formID);  // Intake ID
            ctReviewed_TaskProperties.ExtendedProperties.Add(new Guid("a2deeb92-804a-4d37-b0e0-0b4352111574"), formTrackingNumber);  // Intake Tracking Number
            ctReviewed_TaskProperties.ExtendedProperties.Add(new Guid("3f0daace-0560-4bdb-8b03-c3bb82c56252"), ";#" + workflowConfig.ReviewStaffFunction + ";#"); // Staff Function
            ctReviewed_TaskProperties.ExtendedProperties.Add(new Guid("9bad9167-6317-4c2f-b165-be7af7d76f1d"), taskStatusDescription);  // Intake Task Status
            ctReviewed_TaskProperties.ExtendedProperties.Add(new Guid("9d924f10-6e2e-49da-ac89-25637432c8b2"), analyst);    // Intake Analyst
            ctReviewed_TaskProperties.ExtendedProperties.Add(new Guid("752ce062-37c9-417e-bb03-32e4139b896f"), taskNotes);  // Intake Task Notes
            #endregion

            #region Update lists
            // Acitivity Log
            WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, FCSWorkflowTaskStatus.Open, formID, formTrackingNumber);
            #endregion
        }
        #endregion

        #region Event driven activity
        private void otcReviewed_TaskChanged_Invoked(object sender, ExternalDataEventArgs e)
        {
            try
            {
                #region Variables
                workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
                latest_AfterProperties = otcReviewed_AfterProperties;

                string formID = FormID;
                string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File); 
                FCSWorkflowTaskStatus taskStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
                #endregion

                #region Update lists
                // Activity Log
                WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, taskStatus, formID, formTrackingNumber);
                #endregion

            }
            catch (Exception ex)
            {
                WorkflowSupport.LogException(ex);
            }
        }

        private void caReviewed_TaskProcessing_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File); 
            string user = workflowProperties.OriginatorUser.Name;
            float percentageCompleted = latest_AfterProperties.PercentComplete;

            FCSWorkflowTaskStatus taskStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, taskStatus, formID, formTrackingNumber);

            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, percentageCompleted);
            #endregion
        }
        #endregion

        #region State finalization activity
        private void caReviewed_Final_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion
        }
        #endregion
        #endregion

        #region ApprovedState
        #region Task properties
        // Create Task
        public Guid ctApproved_TaskId = default(System.Guid);
        public SPWorkflowTaskProperties ctApproved_TaskProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        // On Task Changed
        public SPWorkflowTaskProperties otcApproved_AfterProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties otcApproved_BeforeProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        #endregion

        #region State initialization activity
        private void caApproved_Init_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string activityLogStatusChange = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Activity_Log_Request_Status_Approved").ToString();
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File); 
            string formUrl = FormUrl;
            string user = workflowProperties.OriginatorUser.Name;
            string requestStatus = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Approved_State_Name").ToString();
            #endregion

            #region Update form library
            // Write to the form libary
            WorkflowSupport.WriteFormValuesToFormLibrary(workflowProperties, formTrackingNumber, formUrl);
            WorkflowSupport.WriteToFormLibraryStatus(workflowProperties, requestStatus);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLog(workflowProperties, user, activityLogStatusChange, formID, formTrackingNumber);

            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListStatus(workflowProperties, formID, requestStatus);
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, float.Parse("0"));
            #endregion

            #region Update form
            // (This method call needs to be last!!! - see http://hristopavlov.wordpress.com/2008/05/14/uploading-a-file-event-receivers-the-file-has-been-modified-by/)

            // Update status
            WorkflowSupport.SetFormStatus(workflowProperties, requestStatus);

            // Update log

            #endregion
        }

        private void ctApproved_CreateTask_MethodInvoking(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string analyst = WorkflowSupport.GetTaskAnalyst(latest_AfterProperties);
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File); 
            string staffFunction = workflowConfig.ApproveStaffFunction;
            string taskDescription;
            taskDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Approve_Service_Request_Task_Description").ToString() + "<br /><br />" + FormLink;
            string taskNotes = WorkflowSupport.GetTaskNotes(latest_AfterProperties);
            string taskStatusDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Task_Status_Open").ToString();
            string taskTitle = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Approve_Service_Request_Task_Title").ToString() + ": " + FormRequestName;
            #endregion

            #region Create a new task
            ctApproved_TaskId = Guid.NewGuid();
            ctApproved_TaskProperties = new SPWorkflowTaskProperties();

            // Base properties
            ctApproved_TaskProperties.Title = taskTitle;
            ctApproved_TaskProperties.StartDate = DateTime.Now;
            ctApproved_TaskProperties.DueDate = DateTime.Now.AddMinutes(60);
            ctApproved_TaskProperties.Description = taskDescription;
            ctApproved_TaskProperties.PercentComplete = 0;

            // Extended properties
            ctApproved_TaskProperties.ExtendedProperties.Add(new Guid("df9a625c-1c2f-425f-9b7c-8dc278433e13"), formID);  // Intake ID
            ctApproved_TaskProperties.ExtendedProperties.Add(new Guid("a2deeb92-804a-4d37-b0e0-0b4352111574"), formTrackingNumber);  // Intake Tracking Number
            ctApproved_TaskProperties.ExtendedProperties.Add(new Guid("3f0daace-0560-4bdb-8b03-c3bb82c56252"), ";#" + staffFunction + ";#"); // Staff Function
            ctApproved_TaskProperties.ExtendedProperties.Add(new Guid("9bad9167-6317-4c2f-b165-be7af7d76f1d"), taskStatusDescription);  // Intake Task Status
            ctApproved_TaskProperties.ExtendedProperties.Add(new Guid("9d924f10-6e2e-49da-ac89-25637432c8b2"), analyst);    // Intake Analyst
            ctApproved_TaskProperties.ExtendedProperties.Add(new Guid("752ce062-37c9-417e-bb03-32e4139b896f"), taskNotes);  // Intake Task Notes
            #endregion

            #region Update lists
            // Acitivity Log
            WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, FCSWorkflowTaskStatus.Open, formID, formTrackingNumber);
            #endregion
        }
        #endregion

        #region Event driven activity
        private void otcApproved_TaskChanged_Invoked(object sender, ExternalDataEventArgs e)
        {
            try
            {
                #region Variables
                workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
                latest_AfterProperties = otcApproved_AfterProperties;

                string formID = FormID;
                string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
                FCSWorkflowTaskStatus taskStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
                #endregion

                #region Update lists
                // Activity Log
                WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, taskStatus, formID, formTrackingNumber);
                #endregion

            }
            catch (Exception ex)
            {
                WorkflowSupport.LogException(ex);
            }
        }

        private void caApproved_TaskProcessing_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File); 
            string user = workflowProperties.OriginatorUser.Name;
            float percentageCompleted = latest_AfterProperties.PercentComplete;

            FCSWorkflowTaskStatus taskStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, taskStatus, formID, formTrackingNumber);

            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, percentageCompleted);
            #endregion
        }
        #endregion

        #region State finalization activity
        private void caApproved_Final_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion
        }
        #endregion
        #endregion

        #region ArchivedState
        #region Task properties
        // Create Task
        public Guid ctArchived_TaskId = default(System.Guid);
        public SPWorkflowTaskProperties ctArchived_TaskProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        // On Task Changed
        public SPWorkflowTaskProperties otcArchived_AfterProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public SPWorkflowTaskProperties otcArchived_BeforeProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        #endregion

        #region State initialization activity
        private void caArchived_Init_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            string activityLogStatusChange = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Activity_Log_Request_Status_Archived").ToString();
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string formUrl = FormUrl;
            string user = workflowProperties.OriginatorUser.Name;
            string requestStatus = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Archived_State_Name").ToString();
            #endregion

            #region Update form library
            // Write to the form libary
            WorkflowSupport.WriteFormValuesToFormLibrary(workflowProperties, formTrackingNumber, formUrl);
            WorkflowSupport.WriteToFormLibraryStatus(workflowProperties, requestStatus);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLog(workflowProperties, user, activityLogStatusChange, formID, formTrackingNumber);

            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListStatus(workflowProperties, FormID, requestStatus);
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, float.Parse("0"));
            #endregion

            #region Update form
            // Update status
            WorkflowSupport.SetFormStatus(workflowProperties, requestStatus);

            // Update log

            #endregion
        }

        private void ctArchived_TaskCreated_MethodInvoking(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string analyst = WorkflowSupport.GetTaskAnalyst(latest_AfterProperties);
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string taskDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Archive_Service_Request_Task_Description").ToString() + "<br /><br />" + FormLink;
            string taskNotes = WorkflowSupport.GetTaskNotes(latest_AfterProperties);
            string taskStatusDescription = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Task_Status_Open").ToString();
            string taskTitle = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Archive_Service_Request_Task_Title").ToString() + ": " + FormRequestName;
            #endregion

            #region Create a new task
            ctArchived_TaskId = Guid.NewGuid();
            ctArchived_TaskProperties = new SPWorkflowTaskProperties();

            // Base properties
            ctArchived_TaskProperties.Title = taskTitle;
            ctArchived_TaskProperties.DueDate = DateTime.Now.AddMinutes(60);
            ctArchived_TaskProperties.StartDate = DateTime.Now;
            ctArchived_TaskProperties.Description = taskDescription;
            ctArchived_TaskProperties.PercentComplete = 0;

            // Extended properties
            ctArchived_TaskProperties.ExtendedProperties.Add(new Guid("df9a625c-1c2f-425f-9b7c-8dc278433e13"), formID);  // Intake ID
            ctArchived_TaskProperties.ExtendedProperties.Add(new Guid("a2deeb92-804a-4d37-b0e0-0b4352111574"), formTrackingNumber);  // Intake Tracking Number
            ctArchived_TaskProperties.ExtendedProperties.Add(new Guid("3f0daace-0560-4bdb-8b03-c3bb82c56252"), ";#" + workflowConfig.ArchiveStaffFunction + ";#"); // Staff Function
            ctArchived_TaskProperties.ExtendedProperties.Add(new Guid("9bad9167-6317-4c2f-b165-be7af7d76f1d"), taskStatusDescription);  // Intake Task Status
            ctArchived_TaskProperties.ExtendedProperties.Add(new Guid("9d924f10-6e2e-49da-ac89-25637432c8b2"), analyst);    // Intake Analyst
            ctArchived_TaskProperties.ExtendedProperties.Add(new Guid("752ce062-37c9-417e-bb03-32e4139b896f"), taskNotes);  // Intake Task Notes
            #endregion

            #region Update lists
            WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, FCSWorkflowTaskStatus.Open, formID, formTrackingNumber);
            #endregion
        }
        #endregion

        #region Event driven activity
        private void otcArchived_TaskChanged_Invoked(object sender, ExternalDataEventArgs e)
        {
            try
            {
                #region Variables
                latest_AfterProperties = otcArchived_AfterProperties;
                workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

                string formID = FormID;
                string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);

                FCSWorkflowTaskStatus taskStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
                #endregion

                #region Update the task after properties

                #endregion

                #region Update the activity log
                WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, taskStatus, formID, formTrackingNumber);
                #endregion
            }
            catch (Exception ex)
            {
                WorkflowSupport.LogException("otcArchived_Invoked", ex);
            }
        }

        private void caArchived_TaskCompleted_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion

            // Write to the Records Center
            WorkflowSupport.WriteToRecordsCenter(workflowProperties, false);
        }

        private void caArchived_TaskProcessing_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);

            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string user = workflowProperties.OriginatorUser.Name;
            float percentageCompleted = latest_AfterProperties.PercentComplete;

            FCSWorkflowTaskStatus taskStatus = WorkflowSupport.GetTaskStatus(latest_AfterProperties);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLogTaskChanged(workflowProperties, taskStatus, formID, formTrackingNumber);

            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, percentageCompleted);
            #endregion
        }
        #endregion

        #region State finialization activity
        private void caArchived_Final_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion
        }
        #endregion
        #endregion

        #region ClosedState
        #region State initialization activity
        private void caClosed_Init_NoWorkflow_ExecuteCode(object sender, EventArgs e)
        {
            // Make sure to look at the ServiceRequestSummaryEventReceiver in the FCS_Types project!!!

            #region Variables
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string formUrl = FormUrl;
            string requestStatus = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Closed_State_Name").ToString();
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion

            #region Update form library
            // Write to the form libary
            WorkflowSupport.WriteFormValuesToFormLibrary(workflowProperties, formTrackingNumber, formUrl);
            WorkflowSupport.WriteToFormLibraryStatus(workflowProperties, requestStatus);
            #endregion

            #region Update lists
            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListWorkflowComplete(workflowProperties, formID, true);
            #endregion
        }

        private void caClosed_Init_NoReviewNoApproveWorkflow_ExecuteCode(object sender, EventArgs e)
        {
            // Make sure to look at the ServiceRequestSummaryEventReceiver in the FCS_Types project!!!

            #region Variables
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string formUrl = FormUrl;
            string requestStatus = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Closed_State_Name").ToString();
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion

            #region Update form library
            // Write to the form libary
            WorkflowSupport.WriteFormValuesToFormLibrary(workflowProperties, formTrackingNumber, formUrl);
            WorkflowSupport.WriteToFormLibraryStatus(workflowProperties, requestStatus);
            #endregion

            #region Update lists
            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListWorkflowComplete(workflowProperties, formID, true);
            #endregion
        }

        private void caClosed_Init_ElseWorkflow_ExecuteCode(object sender, EventArgs e)
        {
            #region Variables
            string activityLogStatusChange = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Activity_Log_Request_Status_Closed").ToString();
            string formID = FormID;
            string formTrackingNumber = WorkflowSupport.GetFormTrackingNumber(workflowProperties.Item.File);
            string formUrl = FormUrl;
            string user = workflowProperties.OriginatorUser.Name;
            string requestStatus = HttpContext.GetGlobalResourceObject("fcs_workflows_runtime", "Closed_State_Name").ToString();
            workflowConfig = WorkflowConfig.GetWorkflowConfig(workflowProperties.Web);
            #endregion

            #region Update form library
            // Write to the form libary
            WorkflowSupport.WriteFormValuesToFormLibrary(workflowProperties, formTrackingNumber, formUrl);
            WorkflowSupport.WriteToFormLibraryStatus(workflowProperties, requestStatus);
            #endregion

            #region Update lists
            // Activity Log
            WorkflowSupport.WritieToAuditLog(workflowProperties, user, activityLogStatusChange, formID, formTrackingNumber);

            // Request Summaries
            WorkflowSupport.WriteToRequestSummariesListStatus(workflowProperties, formID, requestStatus);
            WorkflowSupport.WriteToRequestSummariesListPercentageCompleted(workflowProperties, formID, float.Parse("1"));
            WorkflowSupport.WriteToRequestSummariesListClosedDate(workflowProperties, formID, DateTime.Now);
            WorkflowSupport.WriteToRequestSummariesListWorkflowComplete(workflowProperties, formID, true);
            #endregion

            #region Update form
            // (This method call needs to be last!!! - see http://hristopavlov.wordpress.com/2008/05/14/uploading-a-file-event-receivers-the-file-has-been-modified-by/)

            // Update status
            WorkflowSupport.SetFormStatus(workflowProperties, requestStatus);

            // Update log

            #endregion
        }
        #endregion
        #endregion

        #region EndState

        #endregion
        #endregion
    }
}
        